
//
//  TabBarItem.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/13.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit

class TabBarItem: UITabBarItem {
    override var badgeValue: String? {
        didSet{
            let tabBarController = valueForKey("_target") as? UITabBarController
            for tabBarChild in tabBarController!.tabBar.subviews{
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for tabBarButtonChild in tabBarChild.subviews {
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                            for badgeValueChild in tabBarButtonChild.subviews{
                                if badgeValueChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                                    var count: UInt32 = 0
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    for i in 0..<count {
                                        let ivar = ivars[Int(i)]
                                        let name = ivar_getName(ivar)
                                        let type = ivar_getTypeEncoding(ivar)
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        if nameStr == "_image"{
                                            badgeValueChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
}